﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
namespace ChamCongMain.Form
{
    public partial class frmPhanCong : DevExpress.XtraEditors.XtraForm
    {
        ChucNang cn;
        PhanCong pc = new PhanCong();
        public Delegates.RefeshGrid LoadData;
        public frmPhanCong(ChucNang cn, PhanCong pc)
        {
            InitializeComponent();

            CongDoanBLL cdl = new CongDoanBLL();
            CongNhanBLL cnbll = new CongNhanBLL();

            glkCongDoan.Properties.DataSource = cdl.GetAllCongDoan();
            glkCongNhan.Properties.DataSource = cnbll.GetAllCongNhan();
            glkNguoiPhanCong.Properties.DataSource = cnbll.GetAllCongNhan();
            this.cn = cn;
            this.pc = pc;
            if (cn == ChucNang.Them)
            {
                this.Text = "Phân Công Mới";
            }
            else
            {
                this.Text = "Sửa Công Phân Công";
                SetValueForForm();
            }

        }
        void SetValueForForm()
        {
            glkCongDoan.EditValue = pc.CongDoanId;
            glkCongNhan.EditValue = pc.CongNhanId;
            glkNguoiPhanCong.EditValue = pc.NguoiPhanCong;
            nbrNam.Value = (decimal)pc.Nam;
            nbrThang.Value = (decimal)pc.Thang;
            txtGhiChu.Text = pc.GhiChu;

            if (pc.NgayPhanCong != null)
                dateNgayPhanCong.EditValue = pc.NgayPhanCong;
            if (pc.NgayGiao != null)
                dateNgayGiao.EditValue = pc.NgayGiao;
            if (pc.NgayNhan != null)
                dateNgayNhan.EditValue = pc.NgayNhan;
        }
        void ClearForm()
        {
            glkCongDoan.EditValue = string.Empty;
            glkCongNhan.EditValue = string.Empty;
            glkNguoiPhanCong.EditValue = string.Empty;
            nbrNam.Value = 0;
            nbrThang.Value = 0;
            txtGhiChu.Text = string.Empty;
            dateNgayPhanCong.EditValue = string.Empty;
            dateNgayGiao.EditValue = string.Empty;
            dateNgayNhan.EditValue = string.Empty;
            cn = ChucNang.Them;
            pc = null;
        }
        PhanCong GetValueFromForm()
        {

            PhanCong pcc = new PhanCong();
            if (this.cn == ChucNang.Sua)
                pcc.PhanCongId = this.pc.PhanCongId;

            if (glkCongDoan.EditValue != null)
                pcc.CongDoanId = (long)glkCongDoan.EditValue;
            if (glkCongNhan.EditValue != null)
                pcc.CongNhanId = (long)glkCongNhan.EditValue;
            if (glkNguoiPhanCong.EditValue != null)
                pcc.NguoiPhanCong = (long)glkNguoiPhanCong.EditValue;

            if (nbrThang.Value!=null)
                pcc.Thang = (int)nbrThang.Value;
            if (nbrNam.Value != null)
                pcc.Nam = (int)nbrNam.Value;
            pcc.GhiChu = txtGhiChu.Text;
            if (dateNgayPhanCong.EditValue != null)
                pcc.NgayPhanCong = (DateTime)dateNgayPhanCong.EditValue;
            if (dateNgayGiao.EditValue != null)
                pcc.NgayGiao = (DateTime)dateNgayGiao.EditValue;
            if (dateNgayNhan.EditValue != null)
                pcc.NgayNhan = (DateTime)dateNgayNhan.EditValue;
            return pcc;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            PhanCong pc = GetValueFromForm();
            PhanCongBLL pcbll = new PhanCongBLL();
            if (cn == ChucNang.Them)
            {
                pcbll.InsertPhanCong(pc);
                this.Close();
            }
            else
            {
                pcbll.UpdatePhanCong(pc);
                this.Close();
            }
            LoadData(pcbll.GetAllPhanCong());
        }
        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            PhanCong pc = GetValueFromForm();
            PhanCongBLL pcbll = new PhanCongBLL();
            if (cn == ChucNang.Them)
            {
                pcbll.InsertPhanCong(pc);
                this.Close();
            }
            else
            {
                pcbll.UpdatePhanCong(pc);
                this.Close();
            }
            LoadData(pcbll.GetAllPhanCong());
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có muốn thoát không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }
    }
}